package com.erp.module.crm.xXCrm

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.ChaKanTuZhiAvtivity
import com.erp.activityKView.CrmFKView
import com.erp.bean.ClientSchemeBean
import com.erp.myAdapter.FileAdapter
import com.erp.myAdapter.GaugeivGvAdapter1
import com.erp.myFragment.BaseKFragment
import com.erp.permission.PermissionListener
import com.erp.util.Dialogutil
import com.erp.util.Httputil
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import kotlinx.android.synthetic.main.fragment_clientthree.*
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2016/11/2 0002.
 * 方案
 */
@SuppressLint("ValidFragment")
class FragmentClientthree : BaseKFragment, View.OnClickListener {
    lateinit var clientSchemeBean: ClientSchemeBean ;
    lateinit var FKP : CrmFKView;
    constructor(){
    }
    constructor( clientSchemeBean: ClientSchemeBean, FKP: CrmFKView){
        this.clientSchemeBean=clientSchemeBean
        this.FKP = FKP
    }
    private val f = "FragmentClientthree"
    lateinit var gvaapter: GaugeivGvAdapter1
    lateinit var gvfileaapter: FileAdapter
    lateinit var updatedialog: AlertDialog
    private var name: String = ""
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_clientthree
    }

    fun initView() {
        gvfileaapter= FileAdapter(activity!!,clientSchemeBean.listfile,false)
        GvGaugeFile.adapter = gvfileaapter
        gvaapter = GaugeivGvAdapter1(clientSchemeBean.listimgbean, activity!!)
        GvGaugeImg.adapter = gvaapter
        BtBaocun.setOnClickListener(this)
    }

    override fun initData() {
        initView()
        initClick()
        initsData()
    }

    private fun initClick() {
        /**
         * 删除图片
         */
        GvGaugeImg.onItemLongClickListener = AdapterView.OnItemLongClickListener { parent, view, position, id ->
            if(position<clientSchemeBean.listimgbean.size) {
                val listid = ArrayList<String>()
                for (i in 0 until clientSchemeBean.listfid.size) {
                    if (clientSchemeBean.listfid[i] != clientSchemeBean.listimgbean[position].imgid) {
                        listid.add(clientSchemeBean.listfid[i])
                    }
                }
                val jo=xiugai()
                Dialogutil.longitem(activity, listid, "project",jo,FKP)
                true
            }else{
                true
            }
        }
        /**
         * 查看图片
         */
        GvGaugeImg.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            if(position<clientSchemeBean.listimgbean.size) {
                val intent = Intent(activity, ChaKanTuZhiAvtivity::class.java)
                val urls : ArrayList<String> = ArrayList()
                urls.add(clientSchemeBean.listimgbean[position].imguri)
                intent.putStringArrayListExtra("url", urls)
                startActivity(intent)
            }else if(clientSchemeBean.listimgbean.size==position){
                Dialogutil.photodialog(activity, this@FragmentClientthree)
            }
        }
        /**
         * 下载附件
         */
        GvGaugeFile.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            PermissionUtils.isupdate(activity!!, object : PermissionListener {
                override fun onSucceed(requestCode: Int, grantPermissions: List<String>) {
//                    Dialogutil.downloaddialog(clientSchemeBean.filelist[position], activity)
                    Dialogutil.downloaddialog1(clientSchemeBean.listfile[position], activity)
                }

                override fun onFailed(requestCode: Int, deniedPermissions: List<String>) {
                    Myutil.MyTost("没有权限，无法下载")
                }
            })
        }
    }
    override fun onClick(v: View?) {
        when(v){
            BtBaocun -> {
                val jo=xiugai()
                updatedialog= Myutil.showupdatedialog(context, "正在保存", false)
                Httputil.cleanclient(activity, ClientKActivity.getid(),jo,object : OnUserDataInterface {
                    override fun onUserData(data: String?) {
                        updatedialog.dismiss()
                        if(data==null){
                            Myutil.MyTost("保存失败")
                            return
                        }
                        Myutil.MyTost("保存成功")
                        FKP.updata(data)
                    }

                    override fun onNot(throwable: String?) {
                        updatedialog.dismiss()
                        Log.d("TAAAG","throwable="+throwable)
                    }

                })
            }
        }
    }
    private fun initsData() {
        EtSchemeDescription.setText(clientSchemeBean.description)
        if(!isFirstLoad){
            gvfileaapter.Allnotify(clientSchemeBean.listfile)
            gvaapter.Allnotify(clientSchemeBean.listimgbean)
        }
    }
    private fun xiugai(): JSONObject {
        var jo = JSONObject()
        try {
            val customer = JSONObject()
            customer.put("description", EtSchemeDescription.text.toString())
            jo.put("project", customer)
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("TAAAG", "--------------" + e.toString())
        }

        return jo
    }
    fun notifydata() {
        initsData()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Log.d("TAAAG","onActivityResult")
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                100 -> {
                    name = data.getStringExtra("dir")
                    val listid = ArrayList<String>()
                    for (i in 0 until clientSchemeBean.listimgbean.size) {
                        listid.add(clientSchemeBean.listimgbean[i].imgid)
                    }
                    val jo=xiugai()
                    Dialogutil.previewdialog(activity, clientSchemeBean.listfid, name, "project",jo, FKP)
                }
                99 -> {
                    val selectedImage = data.data
                    val filePathColumns = arrayOf(MediaStore.Images.Media.DATA)
                    if(selectedImage != null){
                        val c = context!!.contentResolver.query(selectedImage, filePathColumns, null, null, null)
                        c!!.moveToFirst()
                        val columnIndex = c.getColumnIndex(filePathColumns[0])
                        val imagePath = c.getString(columnIndex)
                        val listid = ArrayList<String>()
                        for (i in 0 until clientSchemeBean.listimgbean.size) {
                            listid.add(clientSchemeBean.listimgbean[i].imgid)
                        }
                        val jo=xiugai()
                        Dialogutil.previewdialog(activity, clientSchemeBean.listfid, imagePath, "project",jo, FKP)
                    }

                }
            }
        }
    }


//    override fun onClick(v: View) {
//        when (v.id) {
//            R.id.TvXiugai -> {
//                val intent = Intent(activity, XgliangchiActivity::class.java)
//                val bundle = Bundle()
//                bundle.putParcelable("liangchi", clientSchemeBean)
//                intent.putExtra("liangchi", bundle)
//                startActivity(intent)
//            }
//            R.id.tv_sahnghcuan -> twodialog()
//        }
//    }
}
